package com.ruoyi.teacher.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.teacher.domain.TProjectAuthor;
import com.ruoyi.teacher.mapper.TProjectMapper;
import com.ruoyi.teacher.domain.TProject;
import com.ruoyi.teacher.service.ITProjectService;

/**
 * 结题/验收项目信息集Service业务层处理
 *
 * @author xiangzy
 * @date 2022-05-10
 */
@Service
public class TProjectServiceImpl implements ITProjectService
{
    @Autowired
    private TProjectMapper tProjectMapper;

    /**
     * 查询结题/验收项目信息集
     *
     * @param recordid 结题/验收项目信息集ID
     * @return 结题/验收项目信息集
     */
    @Override
    public TProject selectTProjectById(Long recordid)
    {
        return tProjectMapper.selectTProjectById(recordid);
    }

    /**
     * 查询结题/验收项目信息集列表
     *
     * @param tProject 结题/验收项目信息集
     * @return 结题/验收项目信息集
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TProject> selectTProjectList(TProject tProject)
    {
        return tProjectMapper.selectTProjectList(tProject);
    }

    /**
     * 新增结题/验收项目信息集
     *
     * @param tProject 结题/验收项目信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTProject(TProject tProject)
    {
        tProject.setCreateTime(DateUtils.getNowDate());
        int rows = tProjectMapper.insertTProject(tProject);
        insertTProjectAuthor(tProject);
        return rows;
    }

    /**
     * 修改结题/验收项目信息集
     *
     * @param tProject 结题/验收项目信息集
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTProject(TProject tProject)
    {
        tProject.setUpdateTime(DateUtils.getNowDate());
        tProjectMapper.deleteTProjectAuthorByPROJECTID(tProject.getRecordid());
        insertTProjectAuthor(tProject);
        return tProjectMapper.updateTProject(tProject);
    }

    /**
     * 批量删除结题/验收项目信息集
     *
     * @param recordids 需要删除的结题/验收项目信息集ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTProjectByIds(Long[] recordids)
    {
        tProjectMapper.deleteTProjectAuthorByPROJECTIDs(recordids);
        return tProjectMapper.deleteTProjectByIds(recordids);
    }

    /**
     * 删除结题/验收项目信息集信息
     *
     * @param recordid 结题/验收项目信息集ID
     * @return 结果
     */
    @Override
    public int deleteTProjectById(Long recordid)
    {
        tProjectMapper.deleteTProjectAuthorByPROJECTID(recordid);
        return tProjectMapper.deleteTProjectById(recordid);
    }

    /**
     * 新增结题/验收项目作者信息集信息
     *
     * @param tProject 结题/验收项目信息集对象
     */
    public void insertTProjectAuthor(TProject tProject)
    {
        List<TProjectAuthor> tProjectAuthorList = tProject.getTProjectAuthorList();
        Long recordid = tProject.getRecordid();
        if (StringUtils.isNotNull(tProjectAuthorList))
        {
            List<TProjectAuthor> list = new ArrayList<TProjectAuthor>();
            for (TProjectAuthor tProjectAuthor : tProjectAuthorList)
            {
                tProjectAuthor.setProjectid(recordid);
                list.add(tProjectAuthor);
            }
            if (list.size() > 0)
            {
                tProjectMapper.batchTProjectAuthor(list);
            }
        }
    }
}
